package com.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.app.bean.Movies;
import com.app.bean.News;
import com.app.service.MovieService;
import com.app.service.NewsService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import static com.app.util.StringUtilsEx.*;

@Controller
@RequestMapping("/news")
public class NewsController {

	@Autowired
	private NewsService service;
	
	
	@RequestMapping("/getById")
	@ResponseBody
	public News getById(String id){
		return	service.getById(id);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			service.delete(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add(News news){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			
			if(StringUtilsEx.isNotEmpty(news.getId())){
				service.update(news);
			}else{
				service.add(news);
			}
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String newsname,Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<News> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = service.list(newsname);
			result.put("rows", list);
			PageInfo<News> pageInfo = new PageInfo<News>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
}
